
<?php

/**
 * 逃单计划
 * @author yjmp
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/auto_login.php');
/*用户数据*/
$user_info = $GLOBALS['db']->getRow('SELECT * FROM '.$GLOBALS['ecs']->table('users').' WHERE wecha_id = "'.$_SESSION['openids'].'"');
if ($user_info) {
    $smarty->assign('user_info',$user_info);
}

/*请求获取*/
$act = empty($_REQUEST['act'])? 'escape' : $_REQUEST['act'];
if ($act == 'test') {
    //返回场景值操作
    $str = 'qrscene_233';
    $id = substr($str, 8);
    var_dump($id);exit;
}
/*逃单计划*/
if ($act == 'escape') {
    $smarty->display('escape_project.dwt');
    exit;
}
/*抽奖*/
elseif ($act == 'luckey') {
    /*
    1.查询用户剩余抽奖次数
    2.如果次数不足，返回提示
    3.如果次数足够进行抽奖并返回结果
     */
    //初始化结果
    $result = array(
    	'msg' => '你没有抽奖机会了！',
    	'lucktype'=>0,
    	'success'=>0,
        'escape_rate'=>$user_info['escape_rate'],
    );
    $luckey_time = $GLOBALS['db']->getOne('SELECT luckey_time FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$user_info['user_id'].'"');
    if ($luckey_time > 0) {
    	//抽奖机会-1
    	$GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET luckey_time = luckey_time - 1 WHERE user_id = "'.$user_info['user_id'].'"');
        //判断免单率是不是100
        if ($user_info['escape_rate'] == '100') {
            $result['lucktype'] = 1;
            $result['msg'] = '恭喜你，中奖了！';
            $result['escape_rate'] = 0;
            //逃单率归零,逃单机会加1
            $GLOBALS['db']->query(' UPDATE '.$GLOBALS['ecs']->table('users').' SET escape_rate = 0, escape_chance = escape_chance + 1 WHERE user_id = "'.$user_info['user_id'].'"');
        }else{
            $result['lucktype'] = 0;
            $result['msg'] = '很遗憾，再接再励！';
        }
    	$result['success'] = 1;

    	/**
    	 * 发放奖励
    	 *
    	 * 
    	 */
    }
    $result = json_encode($result);
    die($result);
}

?>
